package com.patrol.quartz;

import com.patrol.service.UserService;
import com.patrol.service.WorkerService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class XGUserExportJob implements Job {

    private static final Logger logger = LoggerFactory.getLogger(XGUserExportJob.class);

    @Autowired
    private UserService userService;

    @Autowired
    private WorkerService workerService;

    @Override
    public void execute(JobExecutionContext context) {
        logger.info("[导出巡更人员今日到岗情况任务开始]");
        this.userExport();
        logger.info("[导出巡更人员今日到岗情况任务结束]");
    }

    private void userExport() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String time = df.format(new Date());
        int xgUserNum = userService.selectXGUserNum();

        Integer id = workerService.selectXGExport(time);
        if (null == id){
            workerService.addXGTJ(xgUserNum,0,time);
        }else {
            int num = workerService.selectXGDGNum(time);
            workerService.updateXGTJ(id,num);
        }
    }
}
